<template>
  <div id="sidebar-wrap" class="tree-container-left vw4">
    <!--<el-button type="primary" @click="handleCreate()" icon="el-icon-share">添加顶层编码</el-button>-->

    <happy-scroll color="#ccc" size="5" class="default-scrollbar__wrap" left  resize scroll-top="190" hide-horizontal>

      <!--show-checkbox-->
      <el-tree ref="tree"
               :props="props1"
               :data="treedata"
               @node-click="handleNodeClick"
               node-key="_id"
               :indent="indent"
               highlight-current
               auto-expand-parent
               show-checkbox
               :default-checked-keys="checkedKey"
               :default-expanded-keys="expandedKey"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>
          <svg-icon v-if="node.data.codeType=='CODE_END'" style="width:14px;" icon-class="leaf1" />
          <svg-icon v-else icon-class="folder1"/>
            {{ node.label }}/{{ node.data._id }}</span>

        </span>
      </el-tree>
    </happy-scroll>
  </div>
</template>
<script>
  import {disableBtn,  ERRS, SUCS} from '@/utils'
  import {fetchGet, deleteOne} from '@/api/common'

  export default {
    name: "treeCode",
    props: {  //子组件接收父组件中的数据,预定义默认值
      /*      selGetData: {
              type: Array,
              default: []
            },
            searchData: {
              type: Object,
              default: {}
            },
            changeSearchData:Array,  //select框选中后的多级数组
            selectId: {
              type: Array,
              default: []
            },
            levStatus: Number*/
    },
    data() {
      return {
        rootData: [],//根节点
        isMusicOn: false,
        isUnique: true,
        isRouter: true,
        treedata: [],
        // currentNode: {},//当前操作的data
        currentNodeId: '',//跳转过来的id
        selGetData: [],  //多级select 根据level 请求api保存的数组
        indent: 14,
        props1: {
          label: 'explainText',
          children: 'childList',
          isLeaf: 'leaf'
        },
        expandedKey: [],//展开项
        checkedKey: [],//选中项
        choseId:''
      };
    },
    watch: {},

    mounted() {
      this.getList();
    },
    methods: {

      getList() {
        fetchGet(`sparepartscode/codeExplains/all`).then(res => {
          if (res.code === 0) {
            this.treedata = res.data
            //我的任务跳转过来
            if (this.currentNodeId) {
              console.log('预加载id',this.treedata);
              console.log('预加载id',this.currentNodeId);
              this.$nextTick(function(){
                let node = this.$refs.tree.getNode(this.currentNodeId)
                console.log(555,node)
                if(node.isLeaf){
                  console.log('isLeaf')
                  this.handleNodeClick(node.parent.data,1)
                }
                this.handleNodeClick(node.data)
              })

            }

            /*            if (this.currentNode.data) {
                          console.log(this.currentNode);
                          this.$refs.tree.setCurrentNode(this.currentNode); //通过id选中当前节点
                        }*/

          } else {
            ERRS(res.message)
          }
        })
      },


      /*
            appendCode(child) {
              if (child.level === 0) {
                this.getList()
              } else {
                const node = this.currentNode.data;
                if (!node.childList) {
                  this.$set(node, 'childList', []);
                }
                node.childList.push(child);
              }
            },
      */


      //选中项展开和选中保存
      setCheckedKey(id) {
        //取消选中保存 getlist刷新加载
        this.choseId=id
        //展开
        this.$refs.tree.setCheckedKeys([]);
        this.$refs.tree.setChecked(id, true,0); //通过id选中当前勾选状态
        this.$refs.tree.setCurrentKey(id); //通过id选中当前高亮的节点
        this.expandedKey=[id]
        this.checkedKey=[id]
      },
      //节点点击事件
      handleNodeClick(data,isEmit) {
        console.log(3030303,isEmit,data)
        console.log(data)
        this.setCheckedKey(data._id)
        // if(!isEmit)
        this.$emit('treeGetlists',data)
      },

      //我的任务跳转加载
      getNodeSetClick(row) {
        console.log(58585,row)
        this.currentNodeId=row.codeEndId
        console.log(58585,this.currentNodeId)
      },

    }
  }
</script>
